package com.haiyi.software.app.basic;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.L;
import com.haiyi.software.ui.controller.WatingProgressDialog;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

public abstract class ActivityMineBasic extends ActivityBasic {

	private final String str_requestNamegetPerson = "getPerson.php";

	private Boolean isWork = false;

	public Boolean getIsWork() {

		return isWork;
	}

	protected String strUID = null;

	protected Map<String, Object> mapUser = null;

	protected WatingProgressDialog watingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
	}

	@Override
	public synchronized void requestNetworkData(Object... args) {

		// TODO 自动生成的方法存根
		if (!isWork) {
			isWork = true;

			String str_requestURL = str_server + str_requestNamegetPerson;

			AsyncHttpClient client = MainApplication.getInstance()
					.getAsyncHttpClient();

			RequestParams params = new RequestParams();

			params.put("args", getJsonArgs());

			client.setTimeout(60);

			client.post(str_requestURL, params, responseHandler);

		}
	}

	private ResponseHandlerInterface responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			isWork = false;
			try {

				String result = new String(responseBody, "UTF-8");

				L.i("ActivityMineBasic=" + result);

				Gson gson = new Gson();

				JsonParser parser = new JsonParser();

				JsonObject jsonResult = parser.parse(result).getAsJsonObject();

				if (jsonResult.get("state").getAsString().equals("200")) {

					Type type = new TypeToken<Map<String, Object>>() {
					}.getType();

					mapUser = gson.fromJson(jsonResult.get("body"), type);

					mapUser.put("spcontent", jsonResult.get("spcontent")
							.getAsString());
					mapUser.put("uhead", jsonResult.get("uhead")
							.getAsJsonArray());
					mapUser.put("uCovers", jsonResult.get("uCover")
							.getAsJsonArray());

					// 更新mainApplication中的个人信息
					JsonObject jsonObj_returnBody = jsonResult.get("body")
							.getAsJsonObject();
					UserInfo mine = MainApplication.getInstance()
							.getMineUserInfo();
					mine.setApplestate(jsonObj_returnBody.get("applestate")
							.isJsonNull() ? 0 : jsonObj_returnBody.get(
							"applestate").getAsInt());
					mine.setClienttype(jsonObj_returnBody.get("clienttype")
							.isJsonNull() ? "android" : jsonObj_returnBody.get(
							"clienttype").getAsString());
					mine.setEmail(jsonObj_returnBody.get("email").isJsonNull() ? ""
							: jsonObj_returnBody.get("email").getAsString());
					mine.setGold(jsonObj_returnBody.get("gold").isJsonNull() ? 0
							: jsonObj_returnBody.get("gold").getAsInt());
					mine.setLastdatetime(jsonObj_returnBody.get("lastdatetime")
							.isJsonNull() ? "" : jsonObj_returnBody.get(
							"lastdatetime").getAsString());
					mine.setLat(jsonObj_returnBody.get("lat").isJsonNull() ? 0
							: jsonObj_returnBody.get("lat").getAsDouble());
					mine.setLongitude(jsonObj_returnBody.get("longitude")
							.isJsonNull() ? 0 : jsonObj_returnBody.get(
							"longitude").getAsDouble());
					mine.setPhone(jsonObj_returnBody.get("phone").isJsonNull() ? ""
							: jsonObj_returnBody.get("phone").getAsString());
					mine.setPwd(jsonObj_returnBody.get("pwd").getAsString());
					mine.setRid(jsonObj_returnBody.get("rid").getAsString());
					mine.setSpcontent(jsonResult.get("spcontent").getAsString());// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
					mine.setUbirthday(jsonObj_returnBody.get("ubirthday")
							.getAsString());
					mine.setUbody(jsonObj_returnBody.get("ubody").isJsonNull() ? ""
							: jsonObj_returnBody.get("ubody").getAsString());
					mine.setUedu(jsonObj_returnBody.get("uedu").isJsonNull() ? ""
							: jsonObj_returnBody.get("uedu").getAsString());
					mine.setUguid(jsonObj_returnBody.get("uguid").getAsString());

					mine.setUheight(jsonObj_returnBody.get("uheight")
							.isJsonNull() ? "0" : jsonObj_returnBody.get(
							"uheight").getAsString());
					mine.setUid(jsonObj_returnBody.get("uid").getAsString());
					mine.setUmoney(jsonObj_returnBody.get("umoney")
							.isJsonNull() ? "" : jsonObj_returnBody.get(
							"umoney").getAsString());
					mine.setUname(jsonObj_returnBody.get("uname").getAsString());
					mine.setUsername(jsonObj_returnBody.get("username")
							.isJsonNull() ? "" : jsonObj_returnBody.get(
							"username").getAsString());
					mine.setUsex(jsonObj_returnBody.get("usex").getAsString());
					mine.setUstar(jsonObj_returnBody.get("ustar").isJsonNull() ? ""
							: jsonObj_returnBody.get("ustar").getAsString());
					mine.setUwork(jsonObj_returnBody.get("uwork").isJsonNull() ? ""
							: jsonObj_returnBody.get("uwork").getAsString());
					mine.setVgold(jsonObj_returnBody.get("vgold").isJsonNull() ? 0
							: jsonObj_returnBody.get("vgold").getAsInt());
					mine.setZone(jsonObj_returnBody.get("zone").isJsonNull() ? ""
							: jsonObj_returnBody.get("zone").getAsString());
					mine.setHeadBig(jsonObj_returnBody.get("headBig")
							.isJsonNull() ? "" : jsonObj_returnBody.get(
							"headBig").getAsString());

					mine.setHeadSmall(jsonObj_returnBody.get("headSmall")
							.isJsonNull() ? "" : jsonObj_returnBody.get(
							"headSmall").getAsString());

					mine.setAge(jsonObj_returnBody.get("age").isJsonNull() ? ""
							: jsonObj_returnBody.get("age").getAsString());

					JsonArray uCoverAry = jsonResult.getAsJsonArray("uCover");

					String str_ucover = "";

					if (uCoverAry.size() > 0) {

						str_ucover = uCoverAry.get(0).getAsJsonObject()
								.get("phobig").getAsString();
					}

					mine.setuCover(str_ucover);

					// mine.setUheadAll(uheadAll);

					L.i("mapUser", mapUser.toString());
				}

				requestFinish(null);

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
				requestError();
			}

		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {
			isWork = false;
			requestError();
		}

	};

	private String getJsonArgs() {

		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();
		if (myInfo != null) {

			Map<String, Object> validator = new HashMap<String, Object>();

			validator.put("uid", myInfo.getUid());
			validator.put("uname", myInfo.getUname());
			validator.put("pwd", myInfo.getPwd());
			validator.put("uguid", myInfo.getUguid());

			Map<String, Object> body = new HashMap<String, Object>();
			body.put("uid", myInfo.getUid());

			Map<String, Object> args = new HashMap<String, Object>();
			args.put("validator", validator);
			args.put("body", body);

			Gson gson = new Gson();
			String s = gson.toJson(args);

			L.i("argsJson", s);

			return s;
		} else {
			return null;
		}

	}

}
